//
//  SameCityViewController.m
//  Otaku
//
//  Created by lanou3g on 16/5/5.
//  Copyright © 2016年 Liuwx. All rights reserved.
//

#import "SameCityViewController.h"
#import "ZLWBaseModel.h"
#import "ZLW_RecommentCell.h"
#import "ZLW_TextCell.h"
#import "ZLW_VideoCell.h"
#import "ZLW_BaseCell.h"
#import "VideoPlayView.h"
#import "ZLW_RecommentDetailController.h"
@interface SameCityViewController ()<UITableViewDelegate,UITableViewDataSource,VideoPlayViewDelegate,VideoTableViewCellDelegate>

@property (nonatomic, strong) NSMutableArray *SouceData;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) VideoPlayView *playView;
@property (nonatomic, strong) ZLW_VideoCell *currentSelectedCell;
@property (nonatomic, assign) BOOL isFullScreenPlaying;
@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation SameCityViewController
-(void)dealloc
{
    [self.myTableView removeObserver:[self.myTableView defaultHeader] forKeyPath:@"contentOffset"];
}
-(NSMutableArray *)SouceData
{
    if (_SouceData == nil) {
        _SouceData  = [NSMutableArray new];
    }
    return _SouceData;
}
- (UITableView *)myTableView
{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, FUll_VIEW_WIDTH, FUll_CONTENT_HEIGHT - PageBtn) style:UITableViewStylePlain];
    }
    return _myTableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self setupBasic];
    //接收数据
    [self laodNetWorking];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    ///注册
    [self ReisterNib];
    //上拉加载
    [self updata];
}

-(void)setupBasic {
    self.currentPage = 0;
    self.isFullScreenPlaying = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.myTableView.scrollIndicatorInsets = self.myTableView.contentInset;
}
///注册
-(void)ReisterNib
{
    [self.myTableView registerNib:[UINib nibWithNibName:@"ZLW_RecommentCell" bundle:nil] forCellReuseIdentifier:@"ZLW_RecommentModel"];
    
    [self.myTableView registerNib:[UINib nibWithNibName:@"ZLW_TextCell" bundle:nil] forCellReuseIdentifier:@"ZLW_TextModel"];
    
    [self.myTableView registerNib:[UINib nibWithNibName:@"ZLW_VideoCell" bundle:nil] forCellReuseIdentifier:@"ZLW_VideoModel"];
}
//上拉加载
-(void)updata
{
    __weak typeof(self)weakSelf = self;
    [self.myTableView addRefreshWithRefreshViewType:(LORefreshViewTypeHeaderDefault) refreshingBlock:^{
        [weakSelf.SouceData removeAllObjects];
        [weakSelf.myTableView reloadData];
        [weakSelf laodNetWorking];
    }];
    
}
//接收数据
-(void)laodNetWorking
{
    [LWXNetWorkRequestManager requestURL:[NSURL URLWithString:SAMECITY_URL] Method:LWX_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dataDict = jsonDict[@"data"][@"data"];
            for (NSDictionary *itemDict in dataDict) {
                
                
                ZLWBaseModel *model =  [ZLWBaseModel modleWith:itemDict];
                if (model.content != nil) {
                    [self.SouceData addObject:model];
                }
                
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.myTableView reloadData];
                [[self.myTableView defaultHeader] endRefreshing];
                
            });
        }
        
    }];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    if (self.isFullScreenPlaying == NO) {
        [self.playView resetPlayView];
        
    }
    self.navigationController.navigationBar.alpha = 1;
}





-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.SouceData.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZLW_BaseCell *cell = [ZLW_BaseCell cellWithTableView:tableView Model:self.SouceData[indexPath.row] IndexPath:indexPath];
    if ([cell isKindOfClass:[ZLW_VideoCell class]]) {
        ZLW_VideoCell *vcell = (ZLW_VideoCell*)cell;
        vcell.delegate = self;
        vcell.indexPath = indexPath;
        if (vcell.getmodel.mp4_url.length > 0) {
            vcell.getmodel = self.SouceData[indexPath.row];
        }
        
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *modelNameModel = NSStringFromClass([self.SouceData[indexPath.row] class]);
    NSString *modelName = [modelNameModel stringByReplacingOccurrencesOfString:@"Model" withString:@""];
    Class cellClass = NSClassFromString([modelName stringByAppendingString:@"Cell"]);
    
    CGFloat cellHeight = [cellClass heightModel:self.SouceData[indexPath.row]];
    return cellHeight;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZLW_RecommentDetailController *ReVC = [[ZLW_RecommentDetailController alloc] init];
    ReVC.getmodel = self.SouceData[indexPath.row];
    
    [self.navigationController pushViewController:ReVC animated:YES];
}

#pragma mark VideoTableViewCell的代理方法
-(void)clickVideoButton:(NSIndexPath *)indexPath
{
    [self.playView resetPlayView];
    ZLW_VideoCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
    self.currentSelectedCell = cell;
    VideoPlayView *playView = [VideoPlayView videoPlayView];
    ZLW_VideoModel *model = self.SouceData[indexPath.row];
    playView.frame = model.videoFrame;
    [cell addSubview:playView];
    cell.playView = playView;
    self.playView = playView;
    self.playView.delegate = self;
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:model.mp4_url]];
    
    self.playView.playerItem = item;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
